import router from '@/router'
import { getToken } from '@/utils/auth'
import { Message } from 'element-ui'
import store from '@/store'
// 页面跳转进度条
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
router.beforeEach(async (to, from, next) => {
  const token = getToken()
  NProgress.start()
  const whiteLsit = ['/404', '/login']
  if (token) {
    const userInfo = store.getters.userInfo
    if (!userInfo.username) {
      await store.dispatch('user/getUserInfo')
    }
    if (to.path === '/login') {
      NProgress.done()
      Message.info('您已经登录过了，无需在登录')
      next('/')
    } else {
      next()
    }
  } else {
    const bool = whiteLsit.includes(to.path)
    if (bool) {
      next()
    } else {
      Message.info('您还没有登录，请先去登录')
      next(`/login?_topath=${to.path}`)
    }
  }
})
router.afterEach((to, from) => {
  NProgress.done()
})
